﻿@using AC.Code.Config
@using AC.Util
@model wychuan2.com.Areas.admin.Models.DbTools.TableViewModel
@{
    ViewBag.Title = "代码生成工具";
    Layout = "../Shared/_Layout.cshtml";
}

@{
    IList<KeyValuePair<string, string>> lstDaoStyleDesc =  EnumUtils.GetEnumDescriptions(typeof(DaoStyle));
    IList<KeyValuePair<string, string>> lstCallStyleDesc = EnumUtils.GetEnumDescriptions(typeof(CallStyle));
    IList<KeyValuePair<string, string>> lstCodeLayerDesc = EnumUtils.GetEnumDescriptions(typeof(CodeLayer));
}
@section styles{
    <link href="~/Content/plugins/highlighter/shCore.css" rel="stylesheet" />
    <link href="~/Content/plugins/highlighter/shCoreDefault.css" rel="stylesheet" />
}
<input type="hidden" value="@Model.TableHasEditOper" id="tableHasEditOper" />
<input type="hidden" value="@Model.TableHasGenerateCodeOper" id="tableHasGenerateCodeOper" />

<div class="row">
    <div class="col-lg-12">
        <div class="ibox">
            <div class="ibox-content">
                <div class="row">
                    <div class="col-lg-3">
                        <form id="dbSelector" method="POST">
                            <legend>配置条件</legend>
                            <input type="hidden" data-type="1" id="viewType" />
                            <span class="hide alert-danger" id="txtError"></span>
                            <fieldset>
                                <div class="form-group">
                                    <label class="control-label" for="dbServer">选择服务器</label>
                                    <div class="input-group">
                                        <span class="input-group-addon"><i class="fa fa-search"></i></span>
                                        <select id="dbServer" name="dbServer" class="form-control">
                                            @foreach (string serverName in Model.DbServers)
                                            {
                                                <option @(serverName == Model.CurrentDbServer ? @Html.Raw("selected='selected'").ToString() : string.Empty)>@serverName</option>
                                            }
                                        </select>
                                    </div>
                                </div>

                                <div class="form-group">
                                    <label class="control-label" for="txtDbName">选择数据库</label>
                                    <div class="input-group">
                                        <span class="input-group-addon"><i class="fa fa-search"></i></span>
                                        <input type="text" id="txtDbName" name="dbName" class="form-control" value="@Model.CurrentDbName" data-provide="typeahead" data-items="100" data-source="@Model.DbNameSource">
                                    </div>
                                </div>

                                <div class="form-group">
                                    <label for="txtTableName" class="control-label">选择数据表</label>
                                    <div class="input-group">
                                        <span class="input-group-addon"><i class="fa fa-search"></i></span>
                                        <input type="text" id="txtTableName" class="form-control" data-provide="typeahead" data-items="50" data-source="@Model.DbTableSource" />
                                    </div>
                                    <a id="returnTableList" href="javascript:;">返回上一步</a>
                                </div>
                            </fieldset>
                        </form>
                    </div>
                    <div class="col-lg-9" id="contents">
                        @Html.Partial("_DatabaseList", Model)
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<div id="codeResult">
    @Html.Partial("_CodeResult", Model.CodeResult)
</div>

<div id="modalDescEdit" class="modal fade" tabindex="-1" role="dialog" aria-labelledby="accountModalLabel">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                <h4 class="modal-title" id="accountModalLabel">编辑描述信息</h4>
            </div>
            <div class="modal-body">
                <form class="form-horizontal">
                    <div class="form-group">
                        <label class="control-label col-sm-2">表名称</label>
                        <div class="col-sm-10">
                            <input type="text" class="disabled form-control" id="lblTableName" disabled="disabled" placeholder="表名称" />
                        </div>
                    </div>

                    <div class="form-group">
                        <label class="control-label col-sm-2">列名称</label>
                        <div class="col-sm-10">
                            <input type="text" class="disabled form-control" id="lblColumnName" disabled="disabled" placeholder="列名称" />
                        </div>
                    </div>

                    <div class="form-group">
                        <label class="control-label col-sm-2" for="txtDesc">描述</label>
                        <div class="col-sm-10">
                            <textarea class="form-control" id="txtDesc"></textarea>
                        </div>
                    </div>
                </form>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                <button type="button" id="btnSaveDesc" class="btn btn-primary">保存</button>
            </div>
        </div>
    </div>
</div>

<div id="generateCodeSettings" class="modal fade" tabindex="-1" role="dialog" aria-labelledby="codeGenerateModalLabel">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                <h4 class="modal-title" id="codeGenerateModalLabel">代码生成参数</h4>
            </div>
            <div class="modal-body">
                <form class="form-horizontal">
                    <input type="hidden" id="codeType" value="1" />
                    <div class="form-group">
                        <label class="control-label col-sm-2 col-sm-offset-1" for="codeTableName">所选择表</label>
                        <div class="col-sm-8">
                            <input type="text" class="disabled form-control" id="codeTableName" disabled="disabled" />
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="control-label col-sm-2 col-sm-offset-1">语言</label>
                        <div class="col-sm-8">
                            <select class="form-control" id="ddlCodeLanguage">
                                <option value="1">C#</option>
                                <option value="2">Java</option>
                            </select>
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="control-label col-sm-2 col-sm-offset-1">架构选择</label>
                        <div class="col-sm-8">
                            <select class="form-control" id="ddlCodeLayer">
                                @foreach (var item in lstCodeLayerDesc)
                                {
                                    <option title="@item.Value" value="@item.Key">@(((CodeLayer)Enum.Parse(typeof(CodeLayer), item.Key)).ToString())</option>
                                }
                            </select>
                            <p class="help-inline"></p>
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="control-label col-sm-2 col-sm-offset-1">DAO实现</label>
                        <div class="col-sm-8">
                            <select class="form-control" id="ddlDaoStyle">
                                @foreach (var item in lstDaoStyleDesc)
                                {
                                    <option title="@item.Value" value="@item.Key">@(((DaoStyle)Enum.Parse(typeof(DaoStyle), item.Key)).ToString())</option>
                                }
                            </select>
                            <p class="help-inline"></p>
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="control-label col-sm-2 col-sm-offset-1" for="ddlCallStyle">对象创建</label>
                        <div class="col-sm-8">
                            <select class="form-control" id="ddlCallStyle">
                                @foreach (var item in lstCallStyleDesc)
                                {
                                    <option title="@item.Value" value="@item.Key">@(((CallStyle)Enum.Parse(typeof(CallStyle), item.Key)).ToString())</option>
                                }
                            </select>
                            <p class="help-inline"></p>
                        </div>
                    </div>
                </form>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-lg btn-default" data-dismiss="modal">关闭</button>
                <button type="button" class="btn btn-lg btn-primary" id="btnGenerateCode">代码生成</button>
            </div>
        </div>
    </div>
</div>

@section scripts{
    <script type="text/javascript" src="~/Scripts/plugins/highlighter/shCore.js"></script>
    <script type="text/javascript" src="~/Scripts/plugins/highlighter/shBrushCSharp.js"></script>
    <script type="text/javascript" src="~/Scripts/plugins/highlighter/shBrushJava.js"></script>
    <script type="text/javascript" src="~/Scripts/plugins/highlighter/shBrushXml.js"></script>

    <script type="text/javascript" src="~/Scripts/dbtools/tableview.js"></script>
    <script type="text/javascript">
        $(function() {
            $("#ddlCallStyle, #ddlCodeLayer, #ddlDaoStyle").change(function() {
                var title = $(this).find("option:selected").attr("title");
                $(this).next("p").text(title);
            });
            $("#ddlCodeLayer").change();
            $("#ddlCallStyle").change();
            $("#ddlDaoStyle").change();

            $('#generateCodeSettings').on('show.bs.modal', function(event) {
                var button = $(event.relatedTarget); // Button that triggered the modal
                var tableName = button.parent().data("table");
                $("#codeTableName").val(tableName);
            });

            $("#btnGenerateCode").bind("click", function() {
                var dbServer = $("#currentDbServer").val();
                var dbName = $("#currentDbName").val();
                var tableName = $("#codeTableName").val();
                var modelName = tableName;
                $.get("/admin/dbtools/generate",
                    {dbServer:dbServer, dbName:dbName, tableName:tableName, modelName:modelName,
                    callStyle: $("#ddlCallStyle").val(), daoStyle: $("#ddlDaoStyle").val(), codeLayer: $("#ddlCodeLayer").val(),
                    codeType: $("#ddlCodeLanguage").val()
                    },
                    function (resp) {
                        $("#codeResult").html(resp);
                        $("#generateCodeSettings").modal("hide");
                        SyntaxHighlighter.highlight();
                    });
            });
            SyntaxHighlighter.all();
        })
    </script>
}